<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .list-wrap{
            overflow-y: auto;
            position: relative;
        }
        .list{
            position: absolute;
            top: 0;
        }
        ul li {
            height: 30px;
        }
    </style>
</head>
<body>

    <div id="app">
        <div class="list-wrap" ref="listWrap" @scroll="scrollListener">
            <div class="scroll-bar" ref="scrollBar">
                <ul class="list" ref="list"> 
                    <li v-for="val in showList">{{val}}</li>
                </ul>
            </div>
        </div>
    </div>
    <script src="https://unpkg.com/vue@next"></script>
    <script>
        const app = Vue.createApp({
            data(){
                return {
                    list : [], //超长的显示列表
                    itemHeight: 30, //每一列的高度
                    showNum: 10, //显示几条数据
                    start: 0, 
                    end : 10
                }
            }, 
            computed: {
                showList(){
                    console.log( this.start,  this.end)
                    return this.list.slice(this.start, this.end)
                }
            },
            mounted(){
                console.log(1, new Date())
                for(let i=0 ;i<1000000; i++){
                    this.list.push("列表" + i)
                }
                console.log(2, new Date())
                //计算滚动容器高度
                this.$refs.listWrap.style.height = this.itemHeight * this.showNum + 'px'
                this.$refs.scrollBar.style.height = this.itemHeight * this.list.length + 'px'

            },
            methods: {
                scrollListener(){
                    let scrollTop = this.$refs.listWrap.scrollTop
                    this.start = Math.floor(scrollTop / this.itemHeight)
                    this.end = this.start + this.showNum
                    this.$refs.list.style.top = this.start * this.itemHeight + 'px'
                }
            }
            
        })
        app.mount('#app')
    </script>
    
</body>
</html>